package itsm.isperp.framework.upload.impl;

import itsm.isperp.framework.upload.UploadChecker;
import itsm.isperp.framework.upload.UploadPreference;

import javax.servlet.http.HttpServletRequest;

/**
 * {@link UploadChecker}接口的默认实现,提供了文件大小检测机制.
 */
public class DefaultUploadCheckerImpl implements UploadChecker {

	/*
	 * (non-Javadoc)
	 * 
	 * @see itsm.isperp.upload.UploadChecker#check(javax.servlet.http.
	 * HttpServletRequest, itsm.isperp.upload.UploadPreference)
	 */
	public void check(HttpServletRequest request,
			UploadPreference uploadPreference) {
		checkSize(request, uploadPreference);
	}

	private void checkSize(HttpServletRequest request,
			UploadPreference uploadPreference) {
		int requestContentLength = request.getContentLength();
		long maxSizeInByte = uploadPreference.getMaxSizeInByte();
		if (requestContentLength > maxSizeInByte) {
			throw new RuntimeException("上传大小超过限制,允许的大小为"
					+ (maxSizeInByte / 1024) + "KB");
		}
	}

}
